package com.controller;

import com.model.AuthGroup;
import com.model.AuthRule;
import com.model.Const;
import com.model.Manager;
import com.service.AuthGroupService;
import com.service.AuthRuleService;
import com.service.ManagerService;
import com.util.MD5Util;
import com.view.UserExcelView;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/31.
 */
@Controller
public class ManagerController {
    @Resource
    private ManagerService managerService;
    @Resource
    private AuthGroupService authGroupService;
    @Resource
    private AuthRuleService ruleService;
    @Resource
    private AuthGroupService groupService;

    private MD5Util md5Util = new MD5Util();

    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    public String loginByManager(@RequestParam(value = "username",required = false)String username,
                                 @RequestParam(value = "password",required = false)String password,
                                 HttpSession session, HttpServletRequest request){
        password = md5Util.transMD5(password+"pinchengcloud");
        Manager manager = managerService.login(new Manager(username,password));
        if(manager==null){
            request.setAttribute("error",1);
            return "login";
        }
        Integer uid = manager.getId();
        List<AuthGroup> AuthGroups = groupService.getRulesbyuid(new Manager(uid));
        String rules = AuthGroups.get(0).getRules();
        // 名称（超级管理员/普通管理员）
        String title = AuthGroups.get(0).getTitle();
        // 权限id
        Integer gid = AuthGroups.get(0).getId();
        String names = "";
        if(!rules.equals("")){
            for (String id : rules.split(",")) {
                List<AuthRule> AuthRule = ruleService.getNamebyid(new AuthRule(
                        Integer.parseInt(id)));
                names += AuthRule.get(0).getName() + ",";
            }
        }

        // 所有权限的名称
        session.setAttribute(Const.SESSION_AUTHORITY, names);
        // 管理员id(过滤器直接过滤超级管理员的信息)
        session.setAttribute(Const.SESSION_GID, gid);
        session.setAttribute(Const.SESSION_USERNAME, username);
        session.setAttribute(Const.SESSION_TITLE, title);
        return "index";
    }

    @RequestMapping("index.do")
    public String  intoIndex(){
        return "index";
    }

    @RequestMapping("remove")
    public ModelAndView remove(HttpSession session) {
        session.invalidate();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("getManagerList.do")
    public String getManagerList(Model model) {
        List<Manager> managers = this.managerService.getManagerList();
        List<AuthGroup> authGroups = this.authGroupService.getAuthGroupList();
        model.addAttribute("managers", managers);
        model.addAttribute("authGroups", authGroups);
        return "manager";
    }

    @RequestMapping("deleteManager.do")
    public String deleteManager(Model model,
                               @RequestParam(value = "id", required = false) Integer id) {
        this.managerService.deleteManager(new Manager(id));
        this.managerService.deleteAuthbyid(id);
        return getManagerList(model);
    }

    @RequestMapping("saveManager.do")
    public String saveManager(
            Model model,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "groupid", required = false) Integer gid,
            @RequestParam(value = "password", required = false) String password) {
        password = md5Util.transMD5(password + "pinchengcloud");
        this.managerService.insertManager(new Manager(username, password));

        Manager manager = this.managerService.login(new Manager(
                username, password));

        Integer uid = manager.getId();
        ArrayList<Integer> ids = new ArrayList<Integer>();
        ids.add(uid);
        ids.add(gid);
        this.managerService.insertAuthbyid(ids);
        return getManagerList(model);
    }

    @RequestMapping(value = "updateManager.do", method = RequestMethod.POST)
    public String updateMember(
            Model model,
            @RequestParam(value = "uid", required = false) Integer uid,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "password", required = false) String password,
            @RequestParam(value = "status", required = false) Integer status) {
        System.out.println("password" + password);
        if (password != null && password.equals("")) {
            password = null;
        }
        if(password != null){
            password = md5Util.transMD5(password + "pinchengcloud");
        }
        this.managerService.modifyManager(new Manager(uid, username,
                password, status));
        return getManagerList(model);
    }
    @RequestMapping(value="excel.do")
    public ModelAndView export2Excel(){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("用户名");
        titles.add("职能");
        titles.add("积分");
        titles.add("状态");
        dataMap.put("titles", titles);
        List<Manager> userList = managerService.getManagerList();
        dataMap.put("userList", userList);
        UserExcelView erv = new UserExcelView();
        ModelAndView mv = new ModelAndView(erv,dataMap);
        return mv;
    }
}
